package com.shen.my_blog.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shen.my_blog.dao.mapper.ArticleMapper;
import com.shen.my_blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {

    @Async("taskExecutor")
    public void updataArticleViewCount(ArticleMapper articleMapper, Article article) {
        int viewCounts = article.getViewCounts();
        Article articleUpdata = new Article();
        articleUpdata.setViewCounts(viewCounts+1);
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Article::getId,article.getId());
        //设置一个 为了在多线程环境下 线程安全
        wrapper.eq(Article::getViewCounts,article.getViewCounts());
        articleMapper.update(articleUpdata,wrapper);

        try {
            Thread.sleep(5000);
            System.out.println("更新完成。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
